package com.lh.pospi.x_store.app;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.itheima.retrofitutils.HttpHelper;
import com.itheima.retrofitutils.ItheimaHttp;
import com.lany.sp.BuildConfig;
import com.lany.sp.SPHelper;
import com.lh.pospi.x_store.greendao.DaoMaster;
import com.lh.pospi.x_store.greendao.DaoSession;
import com.lh.pospi.x_store.modle.UserBean;
import com.lh.pospi.x_store.utils.Constant;
import com.lh.pospi.x_store.utils.Util;
//import com.tsy.sdk.myokhttp.MyOkHttp;
import com.vondear.rxtool.RxSPTool;
import com.vondear.rxtool.RxTool;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public class MyApp extends Application {
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private static MyApp app;
    private UserBean mUserBean;
//    private MyOkHttp myOkHttp;
    public UserBean getmUserBean() {
        return mUserBean;
    }

    public void setmUserBean(UserBean mUserBean) {
        this.mUserBean = mUserBean;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SPHelper.getInstance().init(this, BuildConfig.DEBUG);
        ItheimaHttp.init(this, "http://192.7.7.7");
        //初始化网络请求
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15000L, TimeUnit.MILLISECONDS)
                .readTimeout(15000L, TimeUnit.MILLISECONDS).build();
//        myOkHttp = new MyOkHttp(okHttpClient);
        RxTool.init(this);
        app = this;
        initDataBase();
        if (RxSPTool.getString(this, Constant.BILL_MARK) == null || RxSPTool.getString(this, Constant.BILL_MARK).isEmpty()) {
            RxSPTool.putString(this,Constant.BILL_MARK,Util.getRandomString(2));  //单据标识符
        }

    }
    //初始化greendao
    private void initDataBase() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "pospi_db", null);
        db = openHelper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }
    public DaoSession getDaoSession() {
        return daoSession;
    }
    public static MyApp getInstance(){
        return app;
    }
//    public MyOkHttp getMyOkHttp() {
//        return myOkHttp;
//    }
}
